﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using DBUtility;

namespace SQLServerDAL
{
    public class ShipmentProgressBase
    {
        public ShipmentProgressBase()
        { }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public string Add(Model.ShipmentProgressBase model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ShipmentProgressId", SqlDbType.UniqueIdentifier, 16, "Output"),
					new SqlParameter("@ShipmentType", SqlDbType.NVarChar,512),
					new SqlParameter("@ShipmentDate", SqlDbType.DateTime),
					new SqlParameter("@ShipmentNumber", SqlDbType.VarChar,256),
					new SqlParameter("@ProgressName", SqlDbType.NVarChar,512),
					new SqlParameter("@Notice", SqlDbType.NVarChar,512),
					new SqlParameter("@NoticeType", SqlDbType.VarChar,256),
					new SqlParameter("@NoticeContent", SqlDbType.NVarChar),
					new SqlParameter("@RegardingObjectId", SqlDbType.VarChar,64),
					new SqlParameter("@Email", SqlDbType.VarChar,256),
					new SqlParameter("@Phone", SqlDbType.VarChar,256),
					new SqlParameter("@RegardingObjectName", SqlDbType.NVarChar,512),
					new SqlParameter("@StatusCode", SqlDbType.SmallInt,2),
                    new SqlParameter("@ReturnValue", SqlDbType.Int) };
            parameters[0].Direction = ParameterDirection.Output; 
            parameters[1].Value = model.ShipmentType;
            parameters[2].Value = model.ShipmentDate;
            parameters[3].Value = model.ShipmentNumber;
            parameters[4].Value = model.ProgressName;
            parameters[5].Value = model.Notice;
            parameters[6].Value = model.NoticeType;
            parameters[7].Value = model.NoticeContent;
            parameters[8].Value = model.RegardingObjectId;
            parameters[9].Value = model.Email;
            parameters[10].Value = model.Phone;
            parameters[11].Value = model.RegardingObjectName;
            parameters[12].Value = model.StatusCode;
            parameters[13].Direction = ParameterDirection.ReturnValue;

            string result = "";
            try
            {
                rowsAffected = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "ShipmentProgressBase_Create", parameters);
                int status = ((parameters[13].Value != null) ? ((int)parameters[13].Value) : -1);
                if (status == 0)
                {
                    result = "succeeded|" + parameters[0].Value;
                }
                else
                    result = status.ToString();
            }
            catch (Exception err)
            {
                result = err.Message; ///procedure error
            }
            return result;
        }
         
        /// <summary>
        ///  更新一条数据
        /// </summary>
        public string Update(Model.ShipmentProgressBase model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ShipmentProgressId", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@ShipmentType", SqlDbType.NVarChar,512),
					new SqlParameter("@ShipmentDate", SqlDbType.DateTime),
					new SqlParameter("@ShipmentNumber", SqlDbType.VarChar,256),
					new SqlParameter("@ProgressName", SqlDbType.NVarChar,512),
					new SqlParameter("@Notice", SqlDbType.NVarChar,512),
					new SqlParameter("@NoticeType", SqlDbType.VarChar,256),
					new SqlParameter("@NoticeContent", SqlDbType.NVarChar),
					new SqlParameter("@RegardingObjectId", SqlDbType.VarChar,64),
					new SqlParameter("@Email", SqlDbType.VarChar,256),
					new SqlParameter("@Phone", SqlDbType.VarChar,256),
					new SqlParameter("@RegardingObjectName", SqlDbType.NVarChar,512),
					new SqlParameter("@StatusCode", SqlDbType.SmallInt,2),
                    new SqlParameter("@ReturnValue", SqlDbType.Int)};
            parameters[0].Value = new Guid(model.ShipmentProgressId);
            parameters[1].Value = model.ShipmentType;
            parameters[2].Value = model.ShipmentDate;
            parameters[3].Value = model.ShipmentNumber;
            parameters[4].Value = model.ProgressName;
            parameters[5].Value = model.Notice;
            parameters[6].Value = model.NoticeType;
            parameters[7].Value = model.NoticeContent;
            parameters[8].Value = model.RegardingObjectId;
            parameters[9].Value = model.Email;
            parameters[10].Value = model.Phone;
            parameters[11].Value = model.RegardingObjectName;
            parameters[12].Value = model.StatusCode;
            parameters[13].Direction = ParameterDirection.ReturnValue;

            string result = "";
            try
            {
                rowsAffected = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "ShipmentProgressBase_Update", parameters);
                int status = ((parameters[13].Value != null) ? ((int)parameters[13].Value) : -1);
                if (status == 0)
                {
                    result = "succeeded";
                }
                else
                    result = status.ToString();
            }
            catch (Exception err)
            {
                result = err.Message; ///procedure error
            }
            return result; 
        }
         
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string ShipmentProgressId)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ShipmentProgressId", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = new Guid(ShipmentProgressId);

            StringBuilder SQL = new StringBuilder();
            SQL.Append("DELETE FROM ShipmentProgressBase WHERE [ShipmentProgressId] = @ShipmentProgressId");

            rowsAffected = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.ShipmentProgressBase GetObjectById(string ShipmentProgressId)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@ShipmentProgressId", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = new Guid(ShipmentProgressId);

            Model.ShipmentProgressBase model = new Model.ShipmentProgressBase();

            StringBuilder SQL = new StringBuilder();
            SQL.Append("SELECT * FROM ShipmentProgressBase WHERE [ShipmentProgressId] = @ShipmentProgressId");

            try
            {
                SqlDataReader rd = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL.ToString(), parameters);
                if (rd.Read())
                {
                    model.ShipmentProgressId = rd["ShipmentProgressId"].ToString();
                    model.ShipmentType = rd["ShipmentType"].ToString();
                    model.ShipmentDate = DateTime.Parse(rd["ShipmentDate"].ToString());
                    model.ShipmentNumber = rd["ShipmentNumber"].ToString();
                    model.ProgressName = rd["ProgressName"].ToString();
                    model.Notice = rd["Notice"].ToString();
                    model.NoticeType = rd["NoticeType"].ToString();
                    model.NoticeContent = rd["NoticeContent"].ToString();
                    model.Phone = rd["Phone"].ToString();
                    model.Email = rd["Email"].ToString();
                    model.RegardingObjectId = rd["RegardingObjectId"].ToString();
                    model.RegardingObjectName = rd["RegardingObjectName"].ToString();
                    model.StatusCode = int.Parse(rd["StatusCode"].ToString());
                }
                rd.Close();
                rd.Dispose();
            }
            catch
            {
                return null;
            }

            return model;
        }
    }
}
